<template>
  <div>
    <el-upload
    ref="upload"
    class="upload-demo"
    :limit="limit"
    :action="uploadImgUrl"
    :on-remove="handleRemove"
    :on-success='onSuccess'
    :before-remove="beforeRemove"
    :before-upload="handleBeforeUpload"
    :file-list="fileList"
    :headers="headers"
    :on-exceed="handleExceed"
    accept="image/jpg,image/jpeg,image/png"
    list-type="picture">
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">只能上传jpg/png/jpeg文件，且不超过{{fileSize}}MB</div>
  </el-upload>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
  export default {
    data() {
      return {
        uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/uploadAttaData", // 上传的图片服务器地址
        fileList:[],
        // fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}],
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        attaIds:[],
      };
    },
     props: {
        value: [String, Object, Array],
        // 图片数量限制
        limit: {
            type: Number,
            default: 5,
        },
        // 大小限制(MB)
        fileSize: {
            type: Number,
            default: 5,
        },
        // 文件类型, 例如['png', 'jpg', 'jpeg']
        fileType: {
            type: Array,
            default: () => ["png", "jpg", "jpeg"],
        },
    },
    methods: {
      // 上传前loading加载
    handleBeforeUpload(file) {
      console.log(file)
      let isImg = false;
      if (this.fileType.length) {
        let fileExtension = "";
        if (file.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        isImg = this.fileType.some(type => {
          if (file.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
      } else {
        isImg = file.type.indexOf("image") > -1;
      }

      if (!isImg) {
        this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`);
        return false;
      }
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
        this.fileList=fileList
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
      handleRemove(file, fileList) {
        this.fileList=fileList
        console.log(file, fileList,this.attaIds);
        this.toParent()
      },
      beforeRemove(file, fileList){
        fileList.map((item,index)=>{
          if(item.uid==file.uid){
            this.attaIds.splice(index,1)
          }
        })
      },
      onSuccess(res){
        this.attaIds.push(res.data.attaId)
        this.toParent()
      },
      toParent(){
        this.$emit('getFileList',this.attaIds)
      },
      clearImage(){
        console.log('clearImage')
        this.$refs.upload.clearFiles()
      },
    }
  }
</script>